package com.info.examples.authentication;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class TestAuthController {

    @GetMapping("/testAuth")
    public String testAuth() {
        RestTemplate template = new RestTemplate();
        Map<String, String> parameterMap = new HashMap<>(1 << 2);
        String name = "zhangsan";
        int age = 18;
        String baseUrl = "/auth";
        String appId = "testAppId";
        long createTime = System.currentTimeMillis();
        String secret = "secretTest";
        parameterMap.put("name", name);
        parameterMap.put("age", String.valueOf(age));
        parameterMap.put("appId", appId);
        parameterMap.put("createTime", String.valueOf(createTime));
        parameterMap.put("secret", secret);
        parameterMap.forEach((k, v) -> log.info("{} = {}", k, v));
        AuthToken authToken = AuthToken.createToken(appId, secret, createTime, baseUrl, parameterMap);
        log.info(authToken.toString());
        String requestUrl = "http://localhost:8080/auth?name=" + name + "&age=" + age +
                "&appId=" + appId + "&createTime=" + createTime + "&token=" + authToken.getToken();
        final String result = template.getForObject(requestUrl, String.class, parameterMap);
        return result;
    }
}
